<script setup lang="ts">
import { addSub } from "@/api/admin";
import { FileUpload } from "@/components/common/FileUpload/index";
import { message } from "@/utils/message";
import { ElMessageBox } from "element-plus";
import { ref } from "vue";
import { formRules } from "./rule";
defineOptions({
  name: "Submit"
});

const submitForm = ref<any>({
  name: "",
  contact: "",
  fileJson: ""
});

const ruleFormRef = ref();
const doSubmit = () => {
  ruleFormRef.value.validate(async (valid, fields) => {
    if (valid) {
      ElMessageBox.confirm(`是否确认提交投稿?`, "系统提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        dangerouslyUseHTMLString: true,
        draggable: true
      }).then(async () => {
        await addSub(submitForm.value);
        ruleFormRef.value.resetFields();
        message("投稿成功", { type: "success" });
      });
    }
  });
};
</script>
<template>
  <div>
    <div class="submit">
      <el-form
        ref="ruleFormRef"
        :model="submitForm"
        :rules="formRules"
        label-width="50"
      >
        <div class="flex">
          <el-form-item prop="name">
            <div class="w-[300px]">
              <div style="line-height: 30px">投稿者名稱</div>
              <el-input
                v-model="submitForm.name"
                placeholder="投稿者名稱"
                clearable
              />
            </div>
          </el-form-item>
          <el-form-item prop="contact">
            <div class="w-[300px]">
              <div style="line-height: 30px">聯絡</div>
              <el-input
                v-model="submitForm.contact"
                placeholder="聯絡"
                clearable
              />
            </div>
          </el-form-item>
        </div>
        <el-form-item prop="fileJson">
          <FileUpload v-model="submitForm.fileJson" :limit="1" :maxSize="10" />
          <!-- <el-upload
            class="w-full"
            drag
            action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
            multiple
          >
            <img
              src="@/assets/upload.png"
              width="60"
              height="48"
              class="mb-3"
            />
            <div class="el-upload__text">
              <em class="!text-[#9dbdba] font-bold">
                Drag & drop files or Browse
              </em>
            </div>
          </el-upload> -->
        </el-form-item>
        <div class="foot-btn">
          <div class="btn block" @click="doSubmit()">提交</div>
        </div>
      </el-form>
    </div>
  </div>
</template>
<style lang="scss" scoped>
.submit {
  font-weight: bold;
  margin-bottom: 100px;
  .cell {
    display: flex;
    align-items: center;
    margin-bottom: 20px;
    .cell-title {
      font-size: 25px;
      font-weight: bold;
      min-width: 100px;
    }
    .cell-content {
      flex: 1;
      margin-left: 35px;
      .file-name {
        padding: 5px 10px;
        border-radius: 2px;
        background-color: #d9d9d9;
        cursor: pointer;
      }
    }
  }
  .foot-btn {
    display: flex;
    justify-content: flex-end;
    .btn {
      color: #9dbdba;
      width: 180px;
      min-height: 62px;
      padding: 10px 15px;
      border: 2px solid #9dbdba;
      border-radius: 8px;
      display: flex;
      align-items: center;
      justify-content: center;
      cursor: pointer;
    }
  }
  .block::before {
    content: "";
    flex-shrink: 0;
    width: 16px;
    height: 16px;
    margin-right: 5px;
    background-color: #9dbdba;
  }
  .block::after {
    content: "";
    flex-shrink: 0;
    width: 16px;
    height: 16px;
    margin-left: 5px;
    background-color: #9dbdba;
  }
}
</style>
